#ifndef TIMELINE_H
#define TIMELINE_H

#include <QGraphicsScene>
#include <QSignalMapper>
#include <QUuid>

#include "marker.h"
#include "range.h"
#include "markeritem.h"

class PositionPointerItem;

class Timeline : public QGraphicsScene
{
    Q_OBJECT

public:
    explicit Timeline(QObject *parent = 0);

    void setLength(qint64 length);
    void setPosition(qint64 position);
    void setMarkers(QList<Marker> markers);
    void setFocusedMarker(Marker marker);
    QList<Marker> selectedMarkers() const;
    void setActiveRange(Range range);
    Marker focusedMarker() const;

public slots:
    void focusPreviousMarker();
    void focusNextMarker();

signals:
    void adjusted(qint64 position);
    void selectedMarkersChanged();
    void focusedMarkerChanged();
    void markerAdjusted(Marker marker);

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent* event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent* event);
    void mouseMoveEvent(QGraphicsSceneMouseEvent* event);

private slots:
    void markerXChanged(QObject* markerObject);

private:
    qreal toSceneXPos(qint64 pos);
    qint64 fromSceneXPos(qreal xPos);

private:
    qint64 m_length;

    PositionPointerItem* m_positionPointer;
    QGraphicsRectItem* m_bar;
    QGraphicsRectItem* m_positionBar;
    QGraphicsRectItem* m_activeRangeBar;

    bool m_adjustingPosition;
    QMap<QUuid, MarkerItem*> m_uuidMarkerItemMap;
    QMap<QUuid, Marker> m_uuidMarkerMap;
    QSignalMapper m_markerItemSignalMapper;
};

#endif // TIMELINE_H
